// ignore_for_file: unrelated_type_equality_checks

import 'dart:ffi';

import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter/src/widgets/placeholder.dart';
import 'package:full_display/common/qxp_color.dart';
import 'package:full_display/common/qxp_style.dart';
import 'package:full_display/page/montage/dialog/m_addClock_dialog.dart';
import 'package:full_display/page/montage/dialog/m_pinjie_add_dialog.dart';
import 'package:full_display/page/montage/montage_provider.dart';
import 'package:full_display/unit/asset_utils/asset_utils.dart';
import 'package:full_display/unit/screen/screen_utility.dart';
import 'package:full_display/unit/widgets/widget_dialog/widget_dialog.dart';
import 'package:provider/provider.dart';

class MPinjieDialogWidget extends StatelessWidget {
  const MPinjieDialogWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [ChangeNotifierProvider(create: (_) => MontageProvider())],
      child: const MPinjieDialog(),
    );
  }
}

class MPinjieDialog extends StatefulWidget {
  const MPinjieDialog({super.key});

  @override
  State<MPinjieDialog> createState() => _MPinjieDialogState();
}

class _MPinjieDialogState extends State<MPinjieDialog> {
  var _montageProvider = MontageProvider();
  @override
  Widget build(BuildContext context) {
    _montageProvider = context.watch<MontageProvider>();
    return Container(
      width: 320.0.W,
      height: 394.0.W,
      decoration: const BoxDecoration(
        gradient: LinearGradient(
            //渐变位置
            begin: Alignment.topCenter, //右上
            end: Alignment.bottomCenter, //左下
            //渐变颜色[始点颜色, 结束颜色]
            colors: [ColorPlate.app_blue, ColorPlate.sub_blue]),
        borderRadius: BorderRadius.all(Radius.circular(8.0)),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Padding(
            padding: EdgeInsets.fromLTRB(10.0.W, 20.0.H, 10.0.W, 20.0.H),
            child: Column(
              children: [
                Row(
                  children: [
                    Expanded(
                      child: Text(
                        "添加拼接",
                        style: StandardTextStyle.textWhite17Style,
                        textAlign: TextAlign.center,
                      ),
                    ),
                    InkWell(
                      onTap: () {
                        showDialog(
                          barrierDismissible: true,
                          context: context,
                          builder: (context) {
                            return DialogWidget(
                              contentWidget: const MPinjieAddDialogWidget(),
                            );
                          },
                        );
                      },
                      child: Image.asset(
                        height: 24.0.H,
                        width: 24.0.W,
                        AssetUtils.getAsset2Image("m_angle_add"),
                      ),
                    ),
                  ],
                ),
                Container(
                  height: 20.0.H,
                ),
                Container(
                  height: 300.0.H,
                  child: GridView.builder(
                    padding: EdgeInsets.zero,
                    scrollDirection: Axis.vertical,
                    physics: const AlwaysScrollableScrollPhysics(),
                    shrinkWrap: true,
                    itemCount: _montageProvider.pinjieList.length,
                    itemBuilder: (con, index) {
                      return pinjieItem(
                        _montageProvider.pinjieList[index],
                        index,
                      );
                    },
                    gridDelegate:
                        const SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 1,
                      crossAxisSpacing: 1,
                      mainAxisSpacing: 10,
                      //宽高比
                      childAspectRatio: 28 / 5,
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget pinjieItem(Map data, int index) {
    return Container(
      // padding: const EdgeInsets.all(5),
      width: 280.0.W,
      height: 50.0.W,
      child: Column(
        children: [
          Row(
            children: [
              Expanded(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(data["name"],
                        style: StandardTextStyle.textWhite17Style,
                        textAlign: TextAlign.center,
                        overflow: TextOverflow.ellipsis),
                    Container(
                      width: 10.0.W,
                    ),
                    Text(data["type"],
                        style: StandardTextStyle.textWhite17Style,
                        textAlign: TextAlign.center,
                        overflow: TextOverflow.ellipsis),
                    Container(
                      width: 10.0.W,
                    ),
                    Text(data["layout"],
                        style: StandardTextStyle.textWhite17Style,
                        textAlign: TextAlign.center,
                        overflow: TextOverflow.ellipsis),
                    Container(
                      width: 10.0.W,
                    ),
                    Text(data["model"],
                        style: StandardTextStyle.textWhite17Style,
                        textAlign: TextAlign.center,
                        overflow: TextOverflow.ellipsis),
                    Container(
                      width: 10.0.W,
                    ),
                  ],
                ),
              ),
              InkWell(
                onTap: () {
                  setState(() {
                    print("===========>${index}");
                    _montageProvider.deletePinjie(index);
                  });
                },
                child: Image.asset(
                  height: 24.0.H,
                  width: 24.0.W,
                  AssetUtils.getAsset2Image("m_delete"),
                ),
              ),
            ],
          ),
          Container(
            height: 5.0.H,
          ),
          Divider(
            height: 1.0.W,
            color: ColorPlate.white, // 分割线颜色
          )
        ],
      ),
    );
  }
}
